<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,Chrome=1">
		<!-- Google Chrome Frame也可以让IE用上Chrome的引擎: -->
		<meta name="renderer" content="webkit">
		<!--国产浏览器高速模式-->
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<title>电力通信管理系统</title>

		<!-- 公共样式 开始 -->
		<link rel="stylesheet" type="text/css" href="../../css/base.css">
		<link rel="stylesheet" type="text/css" href="../../css/iconfont.css">
		<script type="text/javascript" src="../../framework/jquery-1.11.3.min.js"></script>
		<link rel="stylesheet" type="text/css" href="../../layui/css/layui.css">
		<script type="text/javascript" src="../../layui/layui.js"></script>
		<!-- 滚动条插件 -->
		<link rel="stylesheet" type="text/css" href="../../css/jquery.mCustomScrollbar.css">
		<script src="../../framework/jquery-ui-1.10.4.min.js"></script>
		<script src="../../framework/jquery.mousewheel.min.js"></script>
		<script src="../../framework/jquery.mCustomScrollbar.min.js"></script>
		<script src="../../framework/cframe.js"></script><!-- 仅供所有子页面使用 -->
		<!-- 公共样式 结束 -->
		<script type="text/javascript" src="../../framework/frame.js" ></script>
		<style>
			.layui-form{
				margin-right: 30%;
			}
		</style>

	</head>

	<body>
		<div class="cBody">
				<form id="form" action="" method="post" class="layui-form">
					<div class="layui-form-item">
						<label class="layui-form-label">需求单位</label>
						<div class="layui-input-inline shortInput">
							<input  type="text" id="sparepartsname" name="sparepartsname" required lay-verify="sparepartsname" placeholder="请输入需求单位" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">备件品牌</label>
						<div class="layui-input-inline shortInput">
							<input   type="text" id="sparepartbrand" name="sparepartbrand" required lay-verify="sparepartbrand" placeholder="请输入备件品牌" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">备件型号</label>
						<div class="layui-input-inline shortInput">
							<input type="text" id="sparepartmodel" name="sparepartmodel" required lay-verify="sparepartmodel" placeholder="请输入备件型号" autocomplete="off" class="layui-input">
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">资产归属</label>
						<div class="layui-input-block ">
							<select class="layui-input-block" lay-filter="sparepartassetownership" style="margin-left: 0px;" name="sparepartassetownership" id="sparepartassetownership" lay-verify="required">
							</select>
						</div>

					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">网络等级</label>
						<div class="layui-input-block ">
							<select class="layui-input-block" style="margin-left: 0px;" name="sparepartclass" id="sparepartclass" lay-filter="sparepartclass" lay-verify="required">
							</select>
						</div>
					</div>
					<div class="layui-form-item">
						<label class="layui-form-label">备件分类</label>
						<div class="layui-input-block ">
							<select class="layui-input-block" style="margin-left: 0px;" name="sparepartvariety" id="sparepartvariety" lay-filter="sparepartvariety" lay-verify="required">
							</select>
						</div>
					</div>

					<div class="layui-form-item">
						<div class="layui-input-block">
							<button class="layui-btn" id="btn" lay-submit lay-filter="submitBut">立即提交</button>
							<button type="reset" class="layui-btn layui-btn-primary">重置</button>
						</div>
					</div>
				</form>
		</div>
	</body>

<script>
	$(document).ready(function(){
		getSparepartVariety("")
		getSparepartClass("")
		getSparepartassetownerShip("")
	});
	layui.use('form', function() {
		var form = layui.form;
		form.verify({
			sparepartsname: function(value, item){ //value：表单的值、item：表单的DOM对象
				if(value == null || value == ""){
					return '请输入需求单位！';
				}
			},
			sparepartbrand: function(value, item){ //value：表单的值、item：表单的DOM对象
				if(value == null || value == ""){
					return '请输入品牌！';
				}
			},
			sparepartmodel: function(value, item){
				if(value == null || value == ""){
					return '请输入型号！';
				}
			}
		});
		//监听提交
		form.on('submit(submitBut)', function(data) {
			var body=data.field;
			$.ajax({
				type: "post",
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
				dataType: "json",
				url: "http://localhost:8888/SparepartsInfo/save",
				contentType: "application/json;charset=UTF-8",//指定消息请求类型
				data: JSON.stringify(body),//将js对象转成json对象
				success: function (data,status,xhr) {
					if (data.code === 0) {
						layer.msg("添加成功");
						$("#form")[0].reset()
						form.render()
					} else {
						layer.msg(data.msg)
					}
					return false
				},
				error:function () {
					layer.msg("添加失败，请稍后再试")
					return false
				}
			})
            return  false
		});
	});
	function  getSparepartVariety(id) {
		$.ajax({
			url: 'http://localhost:8888/SparepartsInfo/listTypeSparepartVariety',
			dataType: 'json',
			type: 'get',
			success: function (data) {
				console.log(data)
				$.each(data.detail, function (index, item) {
					if(id===""){
						$('#sparepartvariety').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}else{
						$('#sparepartvarietySearch').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}
				});
				if(id===""){
					layui.form.render("select");
				}
			}
		})
	}
	function  getSparepartClass(id) {
		$.ajax({
			url: 'http://localhost:8888/SparepartsInfo/listTypeSparepartClass',
			dataType: 'json',
			type: 'get',
			success: function (data) {
				console.log(data)
				$.each(data.detail, function (index, item) {
					if(id===""){
						$('#sparepartclass').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}else{
						$('#sparepartclassSearch').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}
				});
				if(id===""){
					layui.form.render("select");
				}
			}
		})
	}
	function  getSparepartassetownerShip(id) {
		$.ajax({
			url: 'http://localhost:8888/company/list',
			dataType: 'json',
			type: 'post',
			success: function (data) {
				console.log(data)
				$.each(data.data, function (index, item) {
					if(id===""){
						$('#sparepartassetownership').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}else{
						$('#sparepartassetownershipSearch').append(new Option(item.name, item.id,null,item.id === id));// 下拉菜单里添加元素
					}
				});
				if(id===""){
					layui.form.render("select");
				}
			}
		})
	}
</script>

</html>
